package com.ahrhyj.system.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.ahrhyj.common.core.domain.AjaxResult;
import com.ahrhyj.common.utils.DateUtils;
import com.ahrhyj.system.domain.SmsLog;
import com.ahrhyj.system.mapper.SmsLogMapper;
import com.ahrhyj.system.net.SendSmsApi;
import com.ahrhyj.system.net.SendSmsResult;
import com.ahrhyj.system.service.ISmsLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * smslogService业务层处理
 * 
 * @author sentiment
 * @date 2020-09-11
 */
@Service
public class SmsLogServiceImpl implements ISmsLogService
{
    @Autowired
    private SmsLogMapper smsLogMapper;
    @Autowired
    private SendSmsApi sendSmsApi;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult sendSms(String mobile, String templete, int type){
        SmsLog smsLog = new SmsLog();
        smsLog.setMobile(mobile);
        smsLog.setContent(templete);
        smsLog.setStime(new Date());
        smsLog.setType(type);
        if (type == 1){//短信验证码
            String code = RandomUtil.randomNumbers(6);
            String message = String.format(templete, code);
            smsLog.setCode(String.valueOf(code));
            smsLog.setContent(message);
        }
        SendSmsResult sendSmsResult = sendSmsApi.doSms(smsLog.getMobile(), smsLog.getContent());
        if (sendSmsResult == null){
            return AjaxResult.error("验证码发送失败！");
        }
        smsLog.setSendStatus(sendSmsResult.getReturnStatus().equals("Success") ? 0 : -1);
        smsLog.setReturnMsg(sendSmsResult.getMessage());
        smsLog.setTaskId(sendSmsResult.getTaskID());
        smsLogMapper.insertSmsLog(smsLog);
        return smsLog.getSendStatus() == 0 ? AjaxResult.success("发送成功"): AjaxResult.error(sendSmsResult.getMessage());
    }

    @Override
    public int validSms(String mobile, String code){
        int result = -1;
        if(StringUtils.isBlank(mobile)){
            return result;
        }
        SmsLog smsLog = smsLogMapper.querySendSms(mobile);
        if(smsLog != null){
            if(code.equals(smsLog.getCode())){
                Date createDate = smsLog.getStime();
                Date currDate = new Date();
                if(createDate != null && createDate != null){
                    long diff = currDate.getTime() - createDate.getTime();
                    if(diff > 10*60*1000){ //code在10分钟内有效
                        result = -3;
                    }else{
                        result = 0;
                        smsLog.setSendStatus(1);
                        smsLogMapper.updateSmsLog(smsLog);
                    }
                }
            }
        }else{
            result = -2;
        }
       return result;
    }

    @Override
    public int querySendCountToday(String phone){
        return smsLogMapper.querySendCount(phone, DateUtils.format(DateUtils.getDayStartDate()), DateUtils.format(DateUtils.getDayEndDate()));
    }


    /**
     * 查询smslog
     * 
     * @param id smslogID
     * @return smslog
     */
    @Override
    public SmsLog selectSmsLogById(Long id)
    {
        return smsLogMapper.selectSmsLogById(id);
    }

    /**
     * 查询smslog列表
     * 
     * @param smsLog smslog
     * @return smslog
     */
    @Override
    public List<SmsLog> selectSmsLogList(SmsLog smsLog)
    {
        return smsLogMapper.selectSmsLogList(smsLog);
    }

    /**
     * 新增smslog
     * 
     * @param smsLog smslog
     * @return 结果
     */
    @Override
    public int insertSmsLog(SmsLog smsLog)
    {
        return smsLogMapper.insertSmsLog(smsLog);
    }

    /**
     * 修改smslog
     * 
     * @param smsLog smslog
     * @return 结果
     */
    @Override
    public int updateSmsLog(SmsLog smsLog)
    {
        return smsLogMapper.updateSmsLog(smsLog);
    }

    /**
     * 批量删除smslog
     * 
     * @param ids 需要删除的smslogID
     * @return 结果
     */
    @Override
    public int deleteSmsLogByIds(Long[] ids)
    {
        return smsLogMapper.deleteSmsLogByIds(ids);
    }

    /**
     * 删除smslog信息
     * 
     * @param id smslogID
     * @return 结果
     */
    @Override
    public int deleteSmsLogById(Long id)
    {
        return smsLogMapper.deleteSmsLogById(id);
    }
}
